using System.Collections.Generic;
using System.Xml.XPath;

namespace ICommonLibrary.Xml {

    public static class XPathNavigatorExtention {

        public static List<string> SelectMultiInnerXml(this XPathNavigator nav, string xpath) {
            XPathNodeIterator iter = nav.Select(XPathExpression.Compile(xpath));
            var values = new List<string>();
            while (iter.MoveNext()) {
                XPathNavigator node = iter.Current;
                if (node != null) {
                    values.Add(node.InnerXml);
                }
            }
            return values;
        }

        public static List<string> SelectMultiOuterXml(this XPathNavigator nav, string xpath) {
            XPathNodeIterator iter = nav.Select(XPathExpression.Compile(xpath));
            var values = new List<string>();
            while (iter.MoveNext()) {
                XPathNavigator node = iter.Current;
                if (node != null) {
                    values.Add(node.OuterXml);
                }
            }
            return values;
        }

        public static List<string> SelectMultiValue(this XPathNavigator nav, string xpath) {
            XPathNodeIterator iter = nav.Select(XPathExpression.Compile(xpath));
            var values = new List<string>();
            while (iter.MoveNext()) {
                XPathNavigator node = iter.Current;
                if (node != null) {
                    values.Add(node.Value);
                }
            }
            return values;
        }

        public static string SelectSingleInnerXml(this XPathNavigator nav, string xpath, string def) {
            XPathNavigator node = nav.SelectSingleNode(XPathExpression.Compile(xpath));
            if (node != null) {
                return node.InnerXml;
            }
            return def;
        }

        public static string SelectSingleOuterXml(this XPathNavigator nav, string xpath, string def) {
            XPathNavigator node = nav.SelectSingleNode(XPathExpression.Compile(xpath));
            if (node != null) {
                return node.OuterXml;
            }
            return def;
        }

        public static string SelectSingleValue(this XPathNavigator nav, string xpath, string def) {
            XPathNavigator node = nav.SelectSingleNode(XPathExpression.Compile(xpath));
            if (node != null) {
                return node.Value;
            }
            return def;
        }
        
    }
}